﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Numbers;

namespace System8Equations
{
    public class S8TextParser
    {
        /// <summary>
        /// Parse the string for the info that we need to put into the s8 eqsn.
        /// </summary>
        /// <param name="s8"></param>
        /// <param name="text"></param>
        public void Parse(S8Equations s8, string text)
        {
            ///
            /// if the text is empty, then zero out everything.
            /// 

            if (text.Length == 0)
            {
                ZeroS8(s8);
                return;
            }

            ///
            /// Just go until we are good trying different formats
            /// 

            if (TryParseStandaloneOutput(s8, text))
            {
                return;
            }


        }

        /// <summary>
        /// Attempt to load s8 numbers from a simple format that looks like teh following:
        /// 	  n: b=14814 c=11849 l=667
        ///	  p: b=7623 c=6083 l=349
        ///	  n1: b=6466 c=10001 l=602
        ///	  p1: b=3246 c=5131 l=314
        ///	  n2: b=11818 c=5418 l=472
        ///	  p2: b=6108 c=2848 l=243
        ///	  n12: b=5110 c=4534 l=421
        ///	  p12: b=2572 c=2390 l=216
        ///	  
        /// This is output generated by the stand alone program that gives numbers as a function of binning.
        /// </summary>
        /// <param name="s8"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        private bool TryParseStandaloneOutput(S8Equations s8, string text)
        {
            ///
            /// Tokenize and trim
            /// 

            var allTokens = text.Split(' ');
            var cleanTokens = from t in allTokens select t.Trim();

            ///
            /// Move down the line, looking...
            /// 

            FlavorSample f = null;
            foreach (var t in cleanTokens)
            {
                ///
                /// If trimming killed it...
                /// 

                if (t.Length == 0)
                {
                    continue;
                }
                
                ///
                /// Are we switching to a new number to fill?
                /// 

                if (t.IndexOf(":") > 0)
                {
                    if (t == "n:")
                    {
                        f = s8.n;
                    }
                    else if (t == "p:")
                    {
                        f = s8.p;
                    }
                    else if (t == "n1:")
                    {
                        f = s8.n1;
                    }
                    else if (t == "n2:")
                    {
                        f = s8.n2;
                    }
                    else if (t == "p1:")
                    {
                        f = s8.p1;
                    }
                    else if (t == "p2:")
                    {
                        f = s8.p2;
                    }
                    else if (t == "n12:")
                    {
                        f = s8.n12;
                    }
                    else if (t == "p12:")
                    {
                        f = s8.p12;
                    }
                }
                else if (t.IndexOf("=") > 0)
                {
                    var nameValue = t.Split('=');
                    if (nameValue.Length != 2)
                    {
                        return false;
                    }
                    double n = Convert.ToDouble(nameValue[1]);

                    if (nameValue[0] == "b")
                    {
                        f.Bottom = n;
                    }
                    else if (nameValue[0] == "c")
                    {
                        f.Charm = n;
                    }
                    else if (nameValue[0] == "l")
                    {
                        f.Light = n;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    ///
                    /// If the token isn't an = or a : then we have no clue - and we are going to bail!
                    /// 

                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// Set the S8 to zero.
        /// </summary>
        /// <param name="s8"></param>
        private void ZeroS8(S8Equations s8)
        {
            s8.n.Total = 0;
            s8.p.Total = 0;
            s8.n1.Total = 0;
            s8.n2.Total = 0;
            s8.p1.Total = 0;
            s8.p2.Total = 0;
            s8.n12.Total = 0;
            s8.p12.Total = 0;
        }
    }
}
